Reading image as a variable steps shown below:
library(jpeg)
img <- readJPEG("/Users/Yavuz/Desktop/Dersler/IE423/part1/IMG.jpg")
str(img)
## num [1:512, 1:512, 1:3] 0.4 0.459 0.545 0.588 0.58 ...
hist(img)
dim(img)
## [1] 512 512 3
The structure of the image is numeric with dimension of 3. No null values. #### a.
par(mfrow=c(1,1))
plot(c(0,512),c(0,512),type="n",xlab="",ylab="")
rasterImage(img,0,0,nrow(img),ncol(img))
As can be see on the graphs below, all of the channels are colored among red to yellow tones and also looks very similar to each other. This is what should be expected actually because part 1.3 where we plot the mean of each channels columns we see that they trace each other and take very close values on every column.
matrice1 <- img[ , ,1]
matrice2 <- img[ , ,2]
matrice3 <- img[ , ,3]
rotate <- function(x) {
t(apply(x, 2, rev))
}
par(mfrow=c(3,1))
image(rotate(matrice1))
image(rotate(matrice2))
image(rotate(matrice3))
dev.off()
## null device
## 1
An alternative way to plot the channels is with using rasterImage(). With this way, we can see their individual color contribution to the overall picture.
dummymatrix <- matrix(rep(0,262144),nrow=512,ncol=512)
submat1 <- array(data=c(matrice1,dummymatrix,dummymatrix),dim=c(512,512,3))
submat2 <- array(data=c(dummymatrix, matrice2,dummymatrix),dim=c(512,512,3))
submat3 <- array(data=c(dummymatrix,dummymatrix,matrice3),dim=c(512,512,3))
plot(c(0,512),c(0,512),type="n",xlab="",ylab="")
rasterImage(submat1,0,0,nrow(img),ncol(img))
plot(c(0,512),c(0,512),type="n",xlab="",ylab="")
rasterImage(submat2,0,0,nrow(img),ncol(img))
plot(c(0,512),c(0,512),type="n",xlab="",ylab="")
rasterImage(submat3,0,0,nrow(img),ncol(img))
dev.off()
## null device
## 1
i <- 1
column1 <- matrix(,nrow = 1, ncol=512,byrow=TRUE)
column2 <- matrix(,nrow = 1, ncol=512)
column3 <- matrix(,nrow = 1, ncol=512)
for (i in 1:512) {
column1[1,i] <- mean(matrice1[ ,i])
}
i <- 1
for (i in 1:512) {
column2[1,i] <- mean(matrice2[ ,i])
}
i <- 1
for (i in 1:512) {
column3[1,i] <- mean(matrice3[ ,i])
}
par(mfrow=c(1,1))
x <- c(1:512)
plot(x,column1[1,],type="o",col="blue",pch="o",lty=1,lwd=2,xlab="col index",ylab="density")
points(x,column2[1,],col="red",pch="*")
lines(x,column2[1,],col="red",lty=2,lwd=2)
points(x,column3[1,],col="green",pch="+")
lines(x,column3[1,],col="green",lty=3,lwd=2)
legend(x="topleft",legend=c("Channel1","Channel2","Channel3"), col=c("blue","red","green")
,pch=c("o","*","+"),lty=c(1,2,3), ncol=1)
As plot lines show, each channel has very close density values with respect to their mean and mean values of the columns trace each other. That’s one should not surprise of similar plot images of each column.
While we are subtracting halves, negative results may show up and since it is a density value, they ought to stay in range of [0,1] To satisfy that condition, we have to manipulate them. There are different alternative ways to do that. We preferred normalizing the result by subtracting minimum of the matrix(not spesific column or row)from each cell and dividing it to the range. Result is satisfying. Second alternative could be assigning value 0 to negative ones, third might be adding “1” to them and fourth might be taking their absolute.
i <- 1
j <- 1
submat1 <- matrice1
submat2 <- matrice2
submat3 <- matrice3
for (i in 1:256){
for (j in 1:512)
{submat1[j,i] <- submat1[j,i]-submat1[j,i+256]}
}
i <- 1
j <- 1
for (i in 1:256){
for (j in 1:512)
{submat2[j,i] <- submat2[j,i]-submat2[j,i+256]}
}
i <- 1
j <- 1
for (i in 1:256){
for (j in 1:512)
{submat3[j,i] <- submat3[j,i]-submat3[j,i+256]}
}
i <- 1
j <- 1
##normalizing
dummysub1 <- submat1[1:512,1:256]
dummysub2 <- submat2[1:512,1:256]
dummysub3 <- submat3[1:512,1:256]
min1<-min(dummysub1)
min2<-min(dummysub2)
min3<-min(dummysub3)
max1<-max(dummysub1)
max2<-max(dummysub2)
max3<-max(dummysub3)
for(i in 1:512){
for(j in 1:256){
dummysub1[i,j] <- ((dummysub1[i,j] - min1) / (max1-min1))
dummysub2[i,j] <- ((dummysub2[i,j] - min2) / (max2-min2))
dummysub3[i,j] <- ((dummysub3[i,j] - min3) / (max3-min3))
}
}
submat1[1:512,1:256] <- dummysub1
submat2[1:512,1:256] <- dummysub2
submat3[1:512,1:256] <- dummysub3
subimg <- array(data=c(submat1,submat2,submat3),dim=c(512,512,3))
dim(subimg)
## [1] 512 512 3
plot(c(0,512),c(0,512),type="n",xlab="",ylab="")
rasterImage(subimg,0,0,nrow(subimg),ncol(subimg))
An important shortcoming of the median filter is that its output is always constrained, by definition, to be the median value in the window. In a k×k window, if the number of polluted pixels is very large, then the median computed will be an impulse and the noise will not be removed. On the other hand the center value replaced is not tested to find out if it is an impulse or not. Hence if it is not an impulse but a fine pixel of the image then it is removed unnecessarily. The median filter performs poorly when the intensity of the noise is high.
library(imagine)
x=medianFilter(matrice1,radius=5,times=1)
y=medianFilter(matrice1,radius=11,times=1)
z=medianFilter(matrice1,radius=31,times=1)
k=medianFilter(matrice2,radius=5,times=1)
n=medianFilter(matrice2,radius=11,times=1)
m=medianFilter(matrice2,radius=31,times=1)
a=medianFilter(matrice3,radius=5,times=1)
b=medianFilter(matrice3,radius=11,times=1)
c=medianFilter(matrice3,radius=31,times=1)
require(grDevices)
op <- par(bg = "thistle")
plot(c(0, 2000), c(0, 2000), type = "n", xlab = "", ylab = "")
rasterImage(x, 100, 100, 612, 612, interpolate = FALSE)
rasterImage(y, 712, 100, 1224, 612, interpolate = FALSE)
rasterImage(z,1324 , 100, 1836,612, interpolate = FALSE)
rasterImage(a, 100, 712, 612, 1224, interpolate = FALSE)
rasterImage(b, 712, 712, 1224, 1224, interpolate = FALSE)
rasterImage(c,1324 , 712, 1836,1224, interpolate = FALSE)
rasterImage(k, 100, 1324, 612, 1836, interpolate = FALSE)
rasterImage(n, 712, 1324, 1224, 1836, interpolate = FALSE)
rasterImage(m,1324 , 1324, 1836,1836, interpolate = FALSE)
par(op)